package com.jprocms.module.cms.controller.admin.content;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.security.core.LoginUser;
import com.jpro.framework.security.core.util.SecurityFrameworkUtils;
import com.jprocms.module.cms.dal.dataobject.site.SiteDO;
import com.jprocms.module.cms.service.staticpage.StaticHtmlProducer;
import com.jprocms.module.cms.service.staticpage.StaticHtmlService;
import com.jprocms.module.cms.util.CmsContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.jprocms.module.cms.dal.dataobject.task.TaskDO.*;

/**
 * 静态页生成 Controller
 *
 * @author jprocms
 */
@Tag(name = "管理后台 - 静态页生成")
@RestController
@RequestMapping("/cms/content/staticpage")
@Validated
public class ContentStaticPageController {
    private final StaticHtmlService htmlService;
    private final StaticHtmlProducer htmlProducer;

    public ContentStaticPageController(StaticHtmlService htmlService, StaticHtmlProducer htmlProducer) {
        this.htmlService = htmlService;
        this.htmlProducer = htmlProducer;
    }

    @PostMapping("/all")
    @PreAuthorize("@ss.hasPermission('cms:content:staticpage')")
    @Operation(summary = "生成当前站点所有静态页")
    public CommonResult<Boolean> createAllHtml() {
        SiteDO site = CmsContextUtil.getSite();
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        htmlProducer.updateAllHtml(site.getId(), loginUser.getId(), TABLE_NAME_SITE, site);
        return CommonResult.success(true);
    }

    @PostMapping("/index")
    @PreAuthorize("@ss.hasPermission('cms:content:staticpage')")
    @Operation(summary = "生成当前站点首页静态页")
    public CommonResult<Boolean> createIndexHtml() throws Exception {
        SiteDO site = CmsContextUtil.getSite();
        htmlService.updateIndexHtml(site);
        return CommonResult.success(true);
    }

    @PostMapping("/channel")
    @PreAuthorize("@ss.hasPermission('cms:content:staticpage')")
    @Operation(summary = "生成当前站点栏目静态页")
    public CommonResult<Boolean> createChannelHtml() {
        SiteDO site = CmsContextUtil.getSite();
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        htmlProducer.updateChannelHtml(site.getId(), loginUser.getId(), TABLE_NAME_CHANNEL, site);
        return CommonResult.success(true);
    }

    @PostMapping("/content")
    @PreAuthorize("@ss.hasPermission('cms:content:staticpage')")
    @Operation(summary = "生成当前站点内容静态页")
    public CommonResult<Boolean> createContentHtml() {
        SiteDO site = CmsContextUtil.getSite();
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        htmlProducer.updateContentHtml(site.getId(), loginUser.getId(), TABLE_NAME_CONTENT, site);
        return CommonResult.success(true);
    }

    @PostMapping("/includepage")
    @PreAuthorize("@ss.hasPermission('cms:content:staticpage')")
    @Operation(summary = "生成当前站点包含页面静态页")
    public CommonResult<Boolean> createIncludeHtml() {
        SiteDO site = CmsContextUtil.getSite();
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        htmlProducer.updateIncludeHtml(site.getId(), loginUser.getId(), TABLE_NAME_INCLUDE_PAGE, site);
        return CommonResult.success(true);
    }

}